using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The TeacherQualtificationTableDB class is responsible for interacting with the database to retrieve and store information
/// about TeacherQualtificationTable objects.
/// </summary>
public class TeacherQualtificationTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TeacherQualtificationTable from the underlying datasource.
/// </summary>
/// <param name="teacherQualtificationKey">The unique TeacherQualtificationKey of the TeacherQualtificationTable in the database.</param>
/// <returns>An TeacherQualtificationTable when the TeacherQualtificationKey was found in the database, or null otherwise.</returns>
public static TeacherQualtificationTable GetItem(int teacherQualtificationKey)
{
TeacherQualtificationTable myTeacherQualtificationTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@teacherQualtificationKey", teacherQualtificationKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTeacherQualtificationTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTeacherQualtificationTable;
}
}

/// <summary>
/// Returns a list with TeacherQualtificationTable objects.
/// </summary>
/// <returns>A generics List with the TeacherQualtificationTable objects.</returns>
public static TeacherQualtificationTableList GetList()
{
TeacherQualtificationTableList tempList = new TeacherQualtificationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

public static TeacherQualtificationTableList GetList(int UserAccountKey)
{
    TeacherQualtificationTableList tempList = new TeacherQualtificationTableList();
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableSelectList", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.HasRows)
                {
                    while (myReader.Read())
                    {
                        tempList.Add(FillDataRecord(myReader));
                    }
                }
                myReader.Close();
            }
        }
    }
    return tempList;
}

/// <summary>
/// Returns a list with TeacherQualtificationTable objects.
/// </summary>
/// <returns>A generics List with the TeacherQualtificationTable objects.</returns>
public static TeacherQualtificationTableList GetListByUserAccountKey(int UserAccountKey)
{
TeacherQualtificationTableList tempList = new TeacherQualtificationTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a TeacherQualtificationTable in the database.
/// </summary>
/// <param name="myTeacherQualtificationTable">The TeacherQualtificationTable instance to save.</param>
/// <returns>The new TeacherQualtificationKey if the TeacherQualtificationTable is new in the database or the existing TeacherQualtificationKey when an item was updated.</returns>
public static int Save(TeacherQualtificationTable myTeacherQualtificationTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTeacherQualtificationTable.TeacherQualtificationKey == -1)
{
    myCommand.Parameters.AddWithValue("@TeacherQualtificationKey", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@TeacherQualtificationKey", myTeacherQualtificationTable.TeacherQualtificationKey);
}
if (myTeacherQualtificationTable.UserAccountKey == null){
    myCommand.Parameters.AddWithValue("@UserAccountKey", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@UserAccountKey", myTeacherQualtificationTable.UserAccountKey);
}
if (myTeacherQualtificationTable.QualificationName == null){
    myCommand.Parameters.AddWithValue("@QualificationName", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@QualificationName", myTeacherQualtificationTable.QualificationName);
}
if (myTeacherQualtificationTable.Organization == null){
myCommand.Parameters.AddWithValue("@Organization", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@Organization", myTeacherQualtificationTable.Organization);
}
if (myTeacherQualtificationTable.Year == null){
    myCommand.Parameters.AddWithValue("@Year", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@Year", myTeacherQualtificationTable.Year);
}
if (myTeacherQualtificationTable.GradeLevel == null){
myCommand.Parameters.AddWithValue("@GradeLevel", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@GradeLevel", myTeacherQualtificationTable.GradeLevel);
}
if (myTeacherQualtificationTable.Others == null){
myCommand.Parameters.AddWithValue("@Others", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@Others", myTeacherQualtificationTable.Others);
}
if (myTeacherQualtificationTable.Status == null){
myCommand.Parameters.AddWithValue("@Status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@Status", myTeacherQualtificationTable.Status);
}
if (myTeacherQualtificationTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@CreatedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@CreatedDateTime", myTeacherQualtificationTable.CreatedDateTime);
}
if (myTeacherQualtificationTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@ModifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@ModifiedDateTime", myTeacherQualtificationTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TeacherQualtificationTable from the database.
/// </summary>
/// <param name="teacherQualtificationKey">The TeacherQualtificationKey of the TeacherQualtificationTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int teacherQualtificationKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherQualtificationTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@teacherQualtificationKey", teacherQualtificationKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TeacherQualtificationTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TeacherQualtificationTable FillDataRecord(IDataRecord myDataRecord )
{
TeacherQualtificationTable myTeacherQualtificationTable = new TeacherQualtificationTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherQualtificationKey")))
{
myTeacherQualtificationTable.TeacherQualtificationKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("TeacherQualtificationKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myTeacherQualtificationTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("QualificationName")))
{
myTeacherQualtificationTable.QualificationName = myDataRecord.GetString(myDataRecord.GetOrdinal("QualificationName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Organization")))
{
myTeacherQualtificationTable.Organization = myDataRecord.GetString(myDataRecord.GetOrdinal("Organization"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Year")))
{
myTeacherQualtificationTable.Year = myDataRecord.GetString(myDataRecord.GetOrdinal("Year"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("GradeLevel")))
{
myTeacherQualtificationTable.GradeLevel = myDataRecord.GetString(myDataRecord.GetOrdinal("GradeLevel"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Others")))
{
myTeacherQualtificationTable.Others = myDataRecord.GetString(myDataRecord.GetOrdinal("Others"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTeacherQualtificationTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTeacherQualtificationTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTeacherQualtificationTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myTeacherQualtificationTable;
}
}
